package com.rc.saas.mini.service.rc;

import com.rc.saas.mini.mapper.rc.RcMemberFlagMapper;
import com.rc.saas.mini.model.enterprise.EnterpriseMember;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.rc.*;
import com.rc.saas.mini.service.enterprise.EnterpriseMemberService;
import com.rc.saas.mini.vo.member.CurrentMemberFlagAllVo;
import com.rc.saas.mini.vo.member.MemberFlagAllVo;
import com.rc.saas.mini.vo.rc.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RcMemberFlagServiceImpl implements RcMemberFlagService {
    @Autowired
    private RcMemberFlagMapper rcMemberFlagMapper;

    @Autowired
    private EnterpriseMemberService enterpriseMemberService;

    @Autowired
    private RcMemberLevelSettingService rcMemberLevelSettingService;

    @Autowired
    private RcEnterpriseLevelSettingService rcEnterpriseLevelSettingService;
    @Autowired
    private RcChannelLevelSettingService rcChannelLevelSettingService;

    @Autowired
    private RcMemberGainRecordService rcMemberGainRecordService;

    @Autowired
    private RcEnterpriseGainRecordService rcEnterpriseGainRecordService;

    @Autowired
    private RcChannelGainRecordService rcChannelGainRecordService;

    @Autowired
    private RcMemberGainGradeRecordService rcMemberGainGradeRecordService;

    @Autowired
    private RcEnterpriseGainGradeRecordService rcEnterpriseGainGradeRecordService;

    @Autowired
    private RcOperatorService rcOperatorService;

    @Autowired
    private RcChannelGainSettingService rcChannelGainSettingService;

    @Autowired
    private RcEnterpriseGainSettingService rcEnterpriseGainSettingService;

    @Autowired
    private RcEnterpriseGainGradeSettingService rcEnterpriseGainGradeSettingService;

    @Autowired
    private RcMemberGainSettingService rcMemberGainSettingService;

    @Autowired
    private RcMemberGainSettingRecordService rcMemberGainSettingRecordService;

    @Autowired
    private RcMemberGainGradeSettingService rcMemberGainGradeSettingService;

    @Override
    public RcMemberFlagMapper getMapper() {
        return rcMemberFlagMapper;
    }

    public void insertByRegister(MemberInfo member) {
        RcMemberFlag rcMemberFlag = new RcMemberFlag();
        BeanUtils.copyProperties(member, rcMemberFlag);
        rcMemberFlag.setCreatedTime(new Date());
        rcMemberFlag.setLastUpdatedTime(new Date());
        rcMemberFlagMapper.insertSelective(rcMemberFlag);
    }

    @Override
    public CurrentMemberFlagAllVo selectCurrentMemberFlagAllVo(MemberInfo member) {
        CurrentMemberFlagAllVo currentMemberFlagAllVo = new CurrentMemberFlagAllVo();
        RcMemberFlag rcMemberFlag = rcMemberFlagMapper.selectByPrimaryKey(member.getMemberId());
        if (rcMemberFlag == null) {
            insertByRegister(member);
            return currentMemberFlagAllVo;
        }
        //会员 会员权益，会员权益挡位
        if (rcMemberFlag.getSubLevel() != null) {
            RcMemberLevelSetting rcMemberLevelSetting = rcMemberLevelSettingService.selectByPrimaryKey(rcMemberFlag.getSubLevel());
            if (rcMemberLevelSetting != null) {
                RcMemberLevelVo rcMemberLevelVo = new RcMemberLevelVo();
                //当前权益配置
                List<RcMemberLevelVo.RcMemberGainSettingRecordVo> rcMemberGainSettingRecordVos = new ArrayList<>();
                //会员权益
                //List<RcMemberGainRecord> rcMemberGainRecordList = rcMemberGainRecordService.listCurrentByMemberId(member.getMemberId());
                //List<RcMemberGainSettingRecord> rcMemberGainSettingRecordList = rcMemberGainSettingRecordService.listCurrentByMemberId(member.getMemberId());
                List<RcMemberGainSetting> rcMemberGainSettings = rcMemberGainSettingService.listByAllWithLevelSub(rcMemberFlag.getSubLevel());

                //RcMemberGainSettingRecord rcMemberGainSettingRecordData = rcMemberGainSettingRecordList.stream().findFirst().orElseGet(() -> null);
                //if (rcMemberGainSettingRecordData != null) {
                //当前权益
                //RcMemberGainSettingRecord rcMemberGainSettingRecord = rcMemberGainSettingRecordList.stream()
                //        .filter(data -> rcMemberLevelSetting.getMemberLevel().equals(data.getMemberLevel())&&)
                //        .findFirst()
                //        .orElseGet(() -> null);

                for (RcMemberGainSetting rcMemberGainSetting : rcMemberGainSettings) {
                    //当前权益
                    RcMemberGainSettingRecord rcMemberGainSettingRecord = new RcMemberGainSettingRecord();
                    BeanUtils.copyProperties(rcMemberGainSetting, rcMemberGainSettingRecord);
                    rcMemberGainSettingRecord.setMemberId(member.getMemberId());

                    RcMemberLevelVo.RcMemberGainSettingRecordVo rcMemberGainSettingRecordVo = new RcMemberLevelVo.RcMemberGainSettingRecordVo();
                    if (rcMemberGainSettingRecord != null) {
                        //2022.08.09
                        if("1".equals(rcMemberGainSetting.getGainType())){
                            //会员权益挡位
                            List<RcMemberGainGradeRecord> currentData = new ArrayList<>();
                            //List<RcMemberGainGradeSetting> rcMemberGainGradeSettingList = rcMemberGainGradeSettingService.listByMemberGainId(rcMemberGainSettingRecord.getMemberGainId());
                            List<RcMemberGainGradeSetting> rcMemberGainGradeSettingList = rcMemberGainGradeSettingService.listByAll();

                            for (RcMemberGainGradeSetting rcMemberGainGradeSetting : rcMemberGainGradeSettingList) {
                                RcMemberGainGradeRecord rcMemberGainGradeRecord = new RcMemberGainGradeRecord();
                                BeanUtils.copyProperties(rcMemberGainGradeSetting, rcMemberGainGradeRecord);
                                rcMemberGainGradeRecord.setMemberId(member.getMemberId());

                                currentData.add(rcMemberGainGradeRecord);
                            }
                            rcMemberGainSettingRecordVo.setRcMemberGainGradeRecords(currentData);
                        }

                        rcMemberGainSettingRecordVo.setRcMemberGainSettingRecord(rcMemberGainSettingRecord);
                    }

                    rcMemberGainSettingRecordVos.add(rcMemberGainSettingRecordVo);
                }


                //}


                //List<RcMemberGainGradeRecord> rcMemberGainGradeRecordList = rcMemberGainGradeRecordService.listCurrentByMemberId(member.getMemberId());
                //
                //List<RcMemberLevelVo.RcMemberGainSettingRecordVo> rcMemberGainSettingRecordVos = new ArrayList<>();
                //
                ////for (RcMemberGainSettingRecord rcMemberGainSettingRecord : rcMemberGainSettingRecordList) {
                ////会员权益挡位
                //RcMemberLevelVo.RcMemberGainSettingRecordVo rcMemberGainSettingRecordVo = new RcMemberLevelVo.RcMemberGainSettingRecordVo();
                //
                //List<RcMemberGainGradeRecord> currentData = rcMemberGainGradeRecordList.stream()
                //        .filter(data -> data.getMemberGainId().equals(rcMemberGainSettingRecord.getMemberGainId()))
                //        .collect(Collectors.toList());
                //
                //rcMemberGainSettingRecordVo.setRcMemberGainGradeRecords(currentData);
                //rcMemberGainSettingRecordVo.setRcMemberGainSettingRecord(rcMemberGainSettingRecord);
                //rcMemberGainSettingRecordVos.add(rcMemberGainSettingRecordVo);
                //}
                rcMemberLevelVo.setRcMemberLevelSetting(rcMemberLevelSetting);
                rcMemberLevelVo.setRcMemberGainSettingRecordVos(rcMemberGainSettingRecordVos);

                currentMemberFlagAllVo.setRcMemberLevelVo(rcMemberLevelVo);

            }
        }
        //企业权益
        if (rcMemberFlag.getEnterpriseId() != null) {

            EnterpriseMember enterpriseMember = enterpriseMemberService.selectByPrimaryKey(rcMemberFlag.getEnterpriseId());
            if (enterpriseMember != null) {
                RcEnterpriseLevelSetting rcEnterpriseLevelSetting = rcEnterpriseLevelSettingService.selectByPrimaryKey(enterpriseMember.getEnterpriseLevel());
                if (rcEnterpriseLevelSetting != null) {
                    //企业权益
                    //List<RcEnterpriseGainRecord> rcEnterpriseGainRecordList = rcEnterpriseGainRecordService.listCurrentByMemberId(rcMemberFlag.getEnterpriseId());
                    //List<RcEnterpriseGainRecord> rcEnterpriseGainRecordList = rcEnterpriseGainRecordService.listCurrentByEnterpriseId(rcMemberFlag.getEnterpriseId());
                    //List<RcEnterpriseGainGradeRecord> rcEnterpriseGainGradeRecordList = rcEnterpriseGainGradeRecordService.listCurrentByMemberId(rcMemberFlag.getEnterpriseId());
                    //List<RcEnterpriseGainGradeRecord> rcEnterpriseGainGradeRecordList = rcEnterpriseGainGradeRecordService.listCurrentByEnterpriseId(rcMemberFlag.getEnterpriseId());
                    List<RcEnterpriseGainSetting> rcEnterpriseGainSettingList =rcEnterpriseGainSettingService.listByEnterpriseLevel(enterpriseMember.getEnterpriseLevel());
                    List<RcEnterpriseGainGradeSetting> rcEnterpriseGainGradeSettingList = rcEnterpriseGainGradeSettingService.listByDefault();

                    List<RcEnterpriseLevelVo.RcEnterpriseGainRecordVo> rcEnterpriseGainRecordVos = new ArrayList<>();
                    //for (RcEnterpriseGainRecord rcEnterpriseGainRecord : rcEnterpriseGainRecordList) {
                    for (RcEnterpriseGainSetting rcEnterpriseGainSetting : rcEnterpriseGainSettingList) {
                        RcEnterpriseGainRecord rcEnterpriseGainRecord=new RcEnterpriseGainRecord();
                        BeanUtils.copyProperties(rcEnterpriseGainSetting,rcEnterpriseGainRecord);
                        //企业权益挡位
                        RcEnterpriseLevelVo.RcEnterpriseGainRecordVo rcEnterpriseGainRecordVo = new RcEnterpriseLevelVo.RcEnterpriseGainRecordVo();
                        List<RcEnterpriseGainGradeSetting> currentData = rcEnterpriseGainGradeSettingList.stream()
                                .filter(data -> data.getEnterpriseGainId().equals(rcEnterpriseGainSetting.getEnterpriseGainId()))
                                .collect(Collectors.toList());
                        List<RcEnterpriseGainGradeRecord> list=new ArrayList<>();
                        for (RcEnterpriseGainGradeSetting rcEnterpriseGainGradeSetting:currentData){
                            RcEnterpriseGainGradeRecord rcEnterpriseGainGradeRecord=new RcEnterpriseGainGradeRecord();
                            BeanUtils.copyProperties(rcEnterpriseGainGradeSetting,rcEnterpriseGainGradeRecord);
                            list.add(rcEnterpriseGainGradeRecord);
                        }

                        rcEnterpriseGainRecordVo.setRcEnterpriseGainGradeRecords(list);
                        rcEnterpriseGainRecordVo.setRcEnterpriseGainRecord(rcEnterpriseGainRecord);
                        rcEnterpriseGainRecordVos.add(rcEnterpriseGainRecordVo);
                    }
                    RcEnterpriseLevelVo rcEnterpriseLevelVo = new RcEnterpriseLevelVo();
                    rcEnterpriseLevelVo.setEnterpriseMember(enterpriseMember);
                    rcEnterpriseLevelVo.setRcEnterpriseLevelSetting(rcEnterpriseLevelSetting);
                    rcEnterpriseLevelVo.setRcEnterpriseGainRecordVos(rcEnterpriseGainRecordVos);
                    currentMemberFlagAllVo.setRcEnterpriseLevelVo(rcEnterpriseLevelVo);
                }
            }

        }
        //渠道商
        if (rcMemberFlag.getChannelLevel() != null) {
            RcChannelLevelSetting rcChannelLevelSetting = rcChannelLevelSettingService.selectByPrimaryKey(rcMemberFlag.getChannelLevel());
            if (rcChannelLevelSetting != null) {
                //渠道商权益
                //List<RcChannelGainRecord> rcChannelGainRecords = rcChannelGainRecordService.listCurrentByMemberId(member.getMemberId());
                List<RcChannelGainSetting> rcChannelGainRecords = rcChannelGainSettingService.listByDefaultWithChannelLevel(rcChannelLevelSetting.getChannelLevel());

                RcChannelLevelVo rcChannelLevelVo = new RcChannelLevelVo();
                rcChannelLevelVo.setRcChannelLevelSetting(rcChannelLevelSetting);
                rcChannelLevelVo.setRcChannelGainRecords(rcChannelGainRecords);
                currentMemberFlagAllVo.setRcChannelLevelVo(rcChannelLevelVo);
            }
        }
        //加盟商
        if (rcMemberFlag.getOperatorCode() != null) {
            RcOperator rcOperator = rcOperatorService.selectByPrimaryKey(Long.parseLong(rcMemberFlag.getOperatorCode()));
            if (rcOperator != null) {
                currentMemberFlagAllVo.setRcOperator(rcOperator);
            }
        }
        return currentMemberFlagAllVo;
    }

    @Override
    public MemberFlagAllVo selectMemberFlagAllVo(MemberInfo member, int type) {
        //0全部，1会员权益，2企业权益，3渠道商权益
        MemberFlagAllVo memberFlagAllVo = new MemberFlagAllVo();
        //当前配置渠道商权益
        if (type == 0 || type == 3) {
            List<RcChannelLevelSetting> rcChannelLevelSettingList = rcChannelLevelSettingService.listByDefault();
            List<RcChannelGainSetting> rcChannelGainSettingList = rcChannelGainSettingService.listByDefault();
            List<RcChannelLevelSettingVo> rcChannelLevelSettingVoList = new ArrayList<>();
            for (RcChannelLevelSetting rcChannelLevelSetting : rcChannelLevelSettingList) {
                RcChannelLevelSettingVo rcChannelLevelSettingVo = new RcChannelLevelSettingVo();
                rcChannelLevelSettingVo.setRcChannelLevelSetting(rcChannelLevelSetting);
                rcChannelLevelSettingVo.setRcChannelGainSettingList(rcChannelGainSettingList.stream().filter(data -> data.getChannelLevel().equals(rcChannelLevelSetting.getChannelLevel())).collect(Collectors.toList()));
                //权益
                rcChannelLevelSettingVoList.add(rcChannelLevelSettingVo);
            }
            memberFlagAllVo.setRcChannelLevelSettingVo(rcChannelLevelSettingVoList);
        }
        //当前配置企业权益
        if (type == 0 || type == 2) {
            List<RcEnterpriseLevelSetting> rcEnterpriseLevelSettingList = rcEnterpriseLevelSettingService.listByDefault();
            List<RcEnterpriseGainSetting> rcEnterpriseGainSettingList = rcEnterpriseGainSettingService.listByDefault();
            List<RcEnterpriseGainGradeSetting> rcEnterpriseGainGradeSettingList = rcEnterpriseGainGradeSettingService.listByDefault();
            List<RcEnterpriseLevelSettingVo> rcEnterpriseLevelSettingVoList = new ArrayList<>();
            for (RcEnterpriseLevelSetting rcEnterpriseLevelSetting : rcEnterpriseLevelSettingList) {
                RcEnterpriseLevelSettingVo rcEnterpriseLevelSettingVo = new RcEnterpriseLevelSettingVo();

                List<RcEnterpriseGainSetting> rcEnterpriseGainSettings = rcEnterpriseGainSettingList.stream().filter(data -> data.getEnterpriseLevel().equals(rcEnterpriseLevelSetting.getEnterpriseLevel())).collect(Collectors.toList());
                List<RcEnterpriseLevelSettingVo.RcEnterpriseGainSettingVo> rcEnterpriseGainSettingVoList = new ArrayList<>();
                for (RcEnterpriseGainSetting rcEnterpriseGainSetting : rcEnterpriseGainSettings) {
                    //档位
                    RcEnterpriseLevelSettingVo.RcEnterpriseGainSettingVo rcEnterpriseGainSettingVo = new RcEnterpriseLevelSettingVo.RcEnterpriseGainSettingVo();

                    rcEnterpriseGainSettingVo.setRcEnterpriseGainSetting(rcEnterpriseGainSetting);
                    rcEnterpriseGainSettingVo.setRcEnterpriseGainGradeSettingList(rcEnterpriseGainGradeSettingList.stream().filter(data -> data.getEnterpriseGainId().equals(rcEnterpriseGainSetting.getEnterpriseGainId())).collect(Collectors.toList()));
                    rcEnterpriseGainSettingVoList.add(rcEnterpriseGainSettingVo);
                }
                //权益
                rcEnterpriseLevelSettingVo.setRcEnterpriseLevelSetting(rcEnterpriseLevelSetting);
                rcEnterpriseLevelSettingVo.setRcEnterpriseGainSettingVoList(rcEnterpriseGainSettingVoList);
                rcEnterpriseLevelSettingVoList.add(rcEnterpriseLevelSettingVo);
            }
            memberFlagAllVo.setRcEnterpriseLevelSettingVo(rcEnterpriseLevelSettingVoList);
        }
        if (type == 0 || type == 1) {
            //当前配置会员权益
            List<RcMemberLevelSettingVo> rcMemberLevelSettingVoList = new ArrayList<>();
            List<RcMemberLevelSetting> rcMemberLevelSettingList = rcMemberLevelSettingService.listByDefault();
            List<RcMemberGainSetting> rcMemberGainSettingList = rcMemberGainSettingService.selectByExample(new RcMemberGainSettingExample());
            List<RcMemberGainGradeSetting> rcMemberGainGradeSettingList = rcMemberGainGradeSettingService.listByAll();
            for (RcMemberLevelSetting rcMemberLevelSetting : rcMemberLevelSettingList) {
                if ("V0".equals(rcMemberLevelSetting.getMemberLevel())) {
                    continue;
                }
                RcMemberLevelSettingVo rcMemberLevelSettingVo = new RcMemberLevelSettingVo();
                List<RcMemberLevelSettingVo.RcMemberGainSettingVo> rcMemberGainSettingVoList = new ArrayList<>();

                List<RcMemberGainSetting> rcMemberGainSettings = rcMemberGainSettingList
                        .stream().filter(data -> data.getLevelSub().equals(rcMemberLevelSetting.getLevelSub())).collect(Collectors.toList());
                for (RcMemberGainSetting rcMemberGainSetting : rcMemberGainSettings) {
                    //档位
                    RcMemberLevelSettingVo.RcMemberGainSettingVo rcMemberGainSettingVo = new RcMemberLevelSettingVo.RcMemberGainSettingVo();
                    rcMemberGainSettingVo.setRcMemberGainSetting(rcMemberGainSetting);
                    if("1".equals(rcMemberGainSetting.getGainType())){
                        //2022.08.09
                        rcMemberGainSettingVo.setRcMemberGainGradeSettingList(rcMemberGainGradeSettingList);
                    }

                    rcMemberGainSettingVoList.add(rcMemberGainSettingVo);
                }
                rcMemberLevelSetting.setRechargeAmountTarget(rcMemberLevelSetting.getRechargeAmount());
                //权益
                rcMemberLevelSettingVo.setRcMemberLevelSetting(rcMemberLevelSetting);
                rcMemberLevelSettingVo.setRcMemberGainSettingVoList(rcMemberGainSettingVoList);
                rcMemberLevelSettingVoList.add(rcMemberLevelSettingVo);

            }
            memberFlagAllVo.setRcMemberLevelSettingVo(rcMemberLevelSettingVoList);
        }
        return memberFlagAllVo;
    }

    @Override
    public void updateByEnterprise(Long memberId) {
        rcMemberFlagMapper.updateByEnterprise(memberId);
    }
}
